class Sprite {

  vec4 halfExtents_;
  int imageReference_;

public:
  Sprite() {}

  Sprite(int textureRef, vec4 halfExtents) {
    halfExtents_ = halfExtents;
    imageReference_ = textureRef;
  }

  void draw(vec4 position, bool blending, shader &shader, texture* tex, mat4 &modelToProjection) {
    if (blending) {
      glEnable(GL_BLEND);
    }

    tex[imageReference_].render(0);

    // set the uniforms
    shader.render(modelToProjection);

    // set the attributes    
    float vertices[4*4] = {
      position[0] - halfExtents_[0], position[1] - halfExtents_[1], 0, 0,
      position[0] + halfExtents_[0], position[1] - halfExtents_[1], 1, 0,
      position[0] + halfExtents_[0], position[1] + halfExtents_[1], 1, 1,
      position[0] - halfExtents_[0], position[1] + halfExtents_[1], 0, 1,
    };

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)vertices );
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(vertices + 2) );
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(2);

    // kick the draw
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    if (blending) {
      glDisable(GL_BLEND);
    }
  }

  // For rotating sprites. WARNING: ONLY WORKS WITH SQUARE SPRITES!
  void draw(vec4 position, bool blending, float angle, shader &shader, texture* tex, mat4 &modelToProjection) {
    if (blending) {
      glEnable(GL_BLEND);
    }

    tex[imageReference_].render(0);

    // set the uniforms
    shader.render(modelToProjection);

    float cosAngleDistance = (halfExtents_[0] * ROOT_2 * cos(PI_BY_4 - angle));
    float sinAngleDistance = (halfExtents_[0] * ROOT_2 * sin(PI_BY_4 - angle));

    // set the attributes    
    float vertices[4*4] = {
      position[0] - cosAngleDistance, position[1] - sinAngleDistance, 0, 0,
      position[0] + sinAngleDistance, position[1] - cosAngleDistance, 1, 0,
      position[0] + cosAngleDistance, position[1] + sinAngleDistance, 1, 1,
      position[0] - sinAngleDistance, position[1] + cosAngleDistance, 0, 1,
    };

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)vertices );
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(vertices + 2) );
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(2);

    // kick the draw
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    if (blending) {
      glDisable(GL_BLEND);
    }
  }
};
